package cn.iocoder.yudao.module.pickbook.service.userreadinghistory;

import cn.iocoder.yudao.module.pickbook.controller.admin.userreadinghistory.vo.UserReadingHistoryPageReqVO;
import cn.iocoder.yudao.module.pickbook.controller.admin.userreadinghistory.vo.UserReadingHistorySaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.userreadinghistory.UserReadingHistoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pickbook.dal.mysql.userreadinghistory.UserReadingHistoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.pickbook.enums.ErrorCodeConstants.*;

/**
 * 用户阅读记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserReadingHistoryServiceImpl implements UserReadingHistoryService {

    @Resource
    private UserReadingHistoryMapper userReadingHistoryMapper;

    @Override
    public Long createUserReadingHistory(UserReadingHistorySaveReqVO createReqVO) {
        // 插入
        UserReadingHistoryDO userReadingHistory = BeanUtils.toBean(createReqVO, UserReadingHistoryDO.class);
        // 手动生成 id
        long id = UUID.randomUUID().getLeastSignificantBits();
        userReadingHistory.setId(id);
        userReadingHistoryMapper.insert(userReadingHistory);

        // 返回
        return userReadingHistory.getId();
    }

    @Override
    public void updateUserReadingHistory(UserReadingHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateUserReadingHistoryExists(updateReqVO.getId());
        // 更新
        UserReadingHistoryDO updateObj = BeanUtils.toBean(updateReqVO, UserReadingHistoryDO.class);
        userReadingHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserReadingHistory(Long id) {
        // 校验存在
        validateUserReadingHistoryExists(id);
        // 删除
        userReadingHistoryMapper.deleteById(id);
    }

    @Override
        public void deleteUserReadingHistoryListByIds(List<Long> ids) {
        // 删除
        userReadingHistoryMapper.deleteByIds(ids);
        }


    private void validateUserReadingHistoryExists(Long id) {
        if (userReadingHistoryMapper.selectById(id) == null) {
            throw exception(USER_READING_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public UserReadingHistoryDO getUserReadingHistory(Long id) {
        return userReadingHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<UserReadingHistoryDO> getUserReadingHistoryPage(UserReadingHistoryPageReqVO pageReqVO) {
        return userReadingHistoryMapper.selectPage(pageReqVO);
    }

}